/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// win_inputsys.h - mouse and keyboard stuff


#pragma once

#include "..\luna_lib.h"
#include <queue>

/*-----------------------------------------------------------------------------
	Input stuff
-----------------------------------------------------------------------------*/

struct	bind_t
	{
		const char	*name;
		EString		cmd1;
		EString		cmd2;
		bool		is_pressed;
	};


struct EInputState_s {
		EInputState_s	( void ) { 
			target_hwnd		= NULL; 
			mouse_func_ref	= false;
			mouse_relative	= false;
			mouse_hide		= false;
			mouse_clip		= false;
			keyboard_scan	= false;
			input_mode		= 0;
			
			keyboard_key	=	0;
			keyboard_char	=	0;
		}
		~EInputState_s	( void ) {  }
		void			*target_hwnd;
		queue<char>		keys;
		queue<char>		chars;
		IInputSystem::MouseInput	mouse;
		
		string			mouse_func;
		int				mouse_func_ref;
		bool			mouse_relative;
		bool			mouse_hide;
		bool			mouse_clip;
		bool			keyboard_scan;
		uint			input_mode;
		
		char			keyboard_key;
		char			keyboard_char;
		
		IInputSystem::S3DMouseInput	s3dmin;
	};
	

class	EWinInputSystem : public IInputSystem {
	public:
							EWinInputSystem		( void );
							~EWinInputSystem	( void );
		virtual void		SetTargetWindow		( void *hwnd );				
		
		virtual void		ProcessInput		( void );	
		virtual void		SetInputMode		( uint flags );				
		virtual uint		GetInputMode		( void ) { return state.input_mode;	}
		virtual bool		IsKeyPressed		( uint code );
		virtual void		GetMouseInput		( MouseInput &mi );
		virtual char		GetKbChar			( void );
		virtual char		GetKbKey			( void );
		
		virtual void		GetS3DMouseInput	( S3DMouseInput *mi );
		
		virtual	void		SaveKeyBindings		( IHrFile file );
		virtual	uint		GetKeyByCmd			( const char *cmd );		
	
	private:
		void		InitKeyNames		( void );
		uint		GetKeyCode			( const char *name );
		
		void		ScanKeyboard		( bool enable_scan );
		void		HandleMouse			( bool show_cursor, bool clip, bool relative );
		bool		IsTargetWndActive	( void );
		
		bind_t	bindlist[256];
		
		void		EnqueueChar			( char ch );
		void		EnqueueKey			( char key );
		
		queue<char>	kbkeys;
		queue<char>	kbchars;
		
		
		EInputState_s	state;

		void			ProcessInput(void *wnd, bool enable_scan, bool relative_mmove, bool clip_cursor, bool show_cursor, IInputSystem::MouseInput &mouse);

	public:		
		static EWinInputSystem	*self;
		static LRESULT	WndProc				( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
		static WNDPROC	OldWndProc;
		
		static void		ProcessWM_INPUTEvent( LPARAM lParam );
		static void		FreeRawInputDevices	( void );
		static BOOL		InitRawDevices		( void );
	
		static int	setMouseRelative	( lua_State *L );
		static int	setMouseHide		( lua_State *L );
		static int	setMouseClip		( lua_State *L );
		static int	setKeyboardScan		( lua_State *L );
		
		static int	bind				( lua_State *L );
		static int	unbind				( lua_State *L );
		static int	unbindAll			( lua_State *L );
};	